// pages/orderSubmit/orderSubmit.js
const api = require('../../api/index'); // 头部引入

Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabtitleName: ["堂食", "自取（打包）", "外卖配送"],
    orderType: 0,
    columns: ['无需餐具', '需要餐具，商家依据餐量提供', '1份', '2份', '3份', '4份', '5份'],
    columnText: "需要餐具，商家依据餐量提供",
    Dindex: 1,
    deliveryTime: "",
    deliveryStatus: 1, //1立即送出  0选择具体时间
    isShowTimePicker: false,
    isShowPicker: false,
    remark: "请填写口味偏好",
    tablewareNumber: 0,
    tablewareStatus: 1,
    selectDate: "",
    ShoppingCartList: [],
    ShoppingCarSumPrice: 0,
    ShoppingCarNumber: 0,
    dabaofei: 0,
    amount: 0,
    phone: "",
    address: {},
    errorPhone: ""
  },
  //切换订单方式
  onClick(event) {
    let orderType = event.detail.name;
    if (orderType == 0) {
      this.setData({
        amount: this.data.ShoppingCarSumPrice
      })
    } else if (orderType == 1) {
      this.setData({
        amount: this.data.ShoppingCarSumPrice + this.data.ShoppingCarNumber
      })
    } else {
      if (this.data.address.id == undefined) {
        this.getDefaultAddress();
      }
      this.setData({
        amount: this.data.ShoppingCarSumPrice + this.data.ShoppingCarNumber + 1
      })
    }
    this.setData({
      orderType
    })
    console.log(orderType);
  },
  //输入手机号时触发
  onInputPhone(event) {
    console.log(event);
    this.setData({
      phone: event.detail
    })
  },
  //手机号输入框失去焦点时触发
  onBlurPhone() {
    let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    let phone = this.data.phone;
    if (!reg_tel.test(phone)) {
      this.setData({
        errorPhone: "请输入正确的手机号！"
      })
    } else {
      this.setData({
        errorPhone: ""
      })
    }
  },
  //清空手机号
  onClearPhone(event) {
    console.log(1111);
    this.setData({
      phone: "",
      errorPhone: "",
    })
  },
  //日期弹出框
  showDeliveryTime() {
    this.setData({
      isShowTimePicker: true
    })
  },
  //餐具弹出框
  showPicker() {
    this.setData({
      isShowPicker: true
    })
  },
  //餐具弹出框确认按钮
  onConfirm(event) {
    console.log(event.detail);
    if (event.detail.value == "需要餐具，商家依据餐量提供") {
      this.setData({
        tablewareNumber: 0,
        tablewareStatus: 1,
        columnText: event.detail.value,
      })
    } else if (event.detail.value == "无需餐具") {
      this.setData({
        tablewareNumber: 0,
        tablewareStatus: 0,
        columnText: event.detail.value,

      })
    } else {
      for (let i = 0; i < this.data.columns.length; i++) {
        if (event.detail.value == this.data.columns[i]) {
          this.setData({
            tablewareNumber: i - 1,
            tablewareStatus: 0,
            columnText: this.data.columns[i],
          })
          break;
        }
      }
    }
    this.setData({
      isShowPicker: false
    })
  },
  //餐具弹出框取消按钮
  onCancel() {
    this.setData({
      isShowPicker: false
    })
  },
  //日期弹出框确认按钮
  onConfirmTime(event) {
    this.getdeliveryTime();
  },
  //日期弹出框取消按钮
  onCancelTime() {
    this.setData({
      isShowTimePicker: false
    })
  },
  //选择订单配送时间
  getdeliveryTime() {
    // console.log(this.picker);
    let dateIndex = this.picker.data.value[0];
    let timeIndex = this.picker.data.value[1];
    let date = this.picker.data.dateArray[dateIndex];
    let time = this.picker.data.timeArray[timeIndex] + ":00";
    let dateTime = date + ' ' + time; //预计送达时间
    console.log(dateTime);
    this.setData({
      isShowTimePicker: false,
      deliveryTime: "预计" + this.picker.data.timeArray[timeIndex] + "送达",
      estimatedDeliveryTime: dateTime
    })
    if (timeIndex == 0 && dateIndex == 0) {
      this.setData({
        deliveryStatus: 1
      })
    } else {
      this.setData({
        deliveryStatus: 0
      })
    }
  },
  //前往备注页面
  goRemark() {
    wx.navigateTo({
      url: '/pages/remark/remark?remark=' + this.data.remark,
    })
  },
  //前往地址页面
  goAddress() {
    wx.navigateTo({
      url: '/pages/address/address?changeAddress=true',
    })
  },
  //立即支付按钮
  onConfirmPay() {
    let data = {};
    console.log(this.data.orderType);
    if (this.data.orderType == 1) {
      let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      let phone = this.data.phone;
      if (this.data.phone == "") {
        this.setData({
          errorPhone: "请填写手机号"
        })
        return;
      } else if (!reg_tel.test(phone)) {
        this.setData({
          errorPhone: "请输入正确的手机号！"
        })
        return;
      }
      //设置自取（打包）数据
      data.phone = this.data.phone;
      data.packAmount = this.data.dabaofei;
      data.tablewareNumber = this.data.tablewareNumber;
      data.tablewareStatus = this.data.tablewareStatus;
    } else if (this.data.orderType == 2) {
      if (this.data.address.id == undefined) {
        wx.showToast({
          title: '请选择收货地址',
          icon: 'error'
        })
        return;
      }
      //设置外卖配送数据
      data.addressBookId = this.data.address.id;
      data.deliveryStatus = this.data.deliveryStatus;
      data.estimatedDeliveryTime = this.data.estimatedDeliveryTime;
      data.packAmount = this.data.dabaofei;
      data.tablewareNumber = this.data.tablewareNumber;
      data.tablewareStatus = this.data.tablewareStatus;
    }
    //设置公共数据
    data.orderType = this.data.orderType;
    data.amount = this.data.amount;
    data.payMethod = 1;
    data.remark = this.data.remark == "请填写口味偏好" ? "" : this.data.remark;
    console.log(data);

    //跳转到订单支付页面
    wx.navigateTo({
      url: '/pages/pay/pay?orderData=' + JSON.stringify(data)
    })
    //调用提交订单接口
    // api.orderSubmit(data).then(res => {
    //   if (res.code == 1) {
    //     wx.navigateTo({
    //       url: '/pages/pay/pay?orderNumber=' + res.data.orderNumber + '&orderAmount=' + res.data.orderAmount,
    //     })
    //   } else {
    //     wx.showToast({
    //       title: res.msg,
    //       icon: 'error'
    //     })
    //   }
    // })
  },
  //获取用户默认地址
  getDefaultAddress() {
    api.getDefaultAddress().then(res => {
      console.log(res);
      if (res.code == 1) {
        this.setData({
          address: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.picker = this.selectComponent("#picker")
    let ShoppingCarSumPrice = JSON.parse(options.ShoppingCarSumPrice)
    let ShoppingCarNumber = JSON.parse(options.ShoppingCarNumber)
    let ShoppingCartList = JSON.parse(options.ShoppingCartList)
    this.setData({
      ShoppingCarSumPrice,
      ShoppingCartList,
      ShoppingCarNumber,
      dabaofei: ShoppingCarNumber,
      amount: ShoppingCarSumPrice
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getdeliveryTime();
    console.log(this.data.ShoppingCarSum);
    console.log(this.data.ShoppingCartList);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})